package com.gwt.client.util.json;

import com.gwt.client.event.RequestJsonEvent;

public class FetchData {
	public native static void getJson(int requestId, String url, RequestJsonEvent.Handler handler)/*-{
		debugger;
		var callback = "callback" + requestId;

		var script = document.createElement("script");
   		script.setAttribute("src", url+callback);
   		script.setAttribute("id","jsonScript");
   		script.setAttribute("type", "text/javascript");
		
		window[callback] = function(jsonObj) {
     		handler.@com.gwt.client.event.RequestJsonEvent.Handler::handleResult(Lcom/google/gwt/core/client/JavaScriptObject;)(jsonObj);
     		window[callback + "done"] = true;
   		}

   		setTimeout(function() {
     		if (!window[callback + "done"]) {
     			window.alert(url+callback);
       			handler.@com.gwt.client.event.RequestJsonEvent.Handler::handleResult(Lcom/google/gwt/core/client/JavaScriptObject;)(null);
     		}

			var jsonScript = document.getElementById('jsonScript');
			if(jsonScript){
				jsonScript.parentNode.removeChild(jsonScript);
			}
     		delete window[callback];
     		delete window[callback + "done"];
   		}, 2000);

   		document.documentElement.firstChild.appendChild(script);
	}-*/;
}